/*
 * Copyright 2012-2019 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.springframework.boot.context.properties.source;

import java.util.function.Predicate;

import org.springframework.util.Assert;

/**
 * 来自 {@link ConfigurationPropertySource} 内容的状态。
 *
 * @author Phillip Webb
 * @since 2.0.0
 */
public enum ConfigurationPropertyState {

	/**
	 * {@link ConfigurationPropertySource} 至少有一个匹配的
	 * {@link ConfigurationProperty}。
	 */
	PRESENT,

	/**
	 * {@link ConfigurationPropertySource} 没有匹配的
	 * {@link ConfigurationProperty ConfigurationProperties}。
	 */
	ABSENT,

	/**
	 * 无法确定 {@link ConfigurationPropertySource} 是否有匹配的
	 * {@link ConfigurationProperty ConfigurationProperties}。
	 */
	UNKNOWN;

	/**
	 * 使用谓词搜索给定的可迭代对象，以确定内容是
	 * {@link #PRESENT} 还是 {@link #ABSENT}。
	 * @param <T> 数据类型
	 * @param source 要搜索的源可迭代对象
	 * @param predicate 用于测试存在性的谓词
	 * @return 如果可迭代对象包含匹配项，返回 {@link #PRESENT}，否则返回 {@link #ABSENT}。
	 */
	static <T> ConfigurationPropertyState search(Iterable<T> source, Predicate<T> predicate) {
		Assert.notNull(source, "Source must not be null");
		Assert.notNull(predicate, "Predicate must not be null");
		for (T item : source) {
			if (predicate.test(item)) {
				return PRESENT;
			}
		}
		return ABSENT;
	}

}
